﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Xpo;
using Component;

namespace Component
{
    public class CategorySubDB:Base
    {
        /// <summary>
        /// public static XPCollection<CategorySub> GetAll()
        /// </summary>
        /// <returns>XPCollection<CategorySub></returns>
        public static XPCollection<CategorySub> GetAll(bool admin)
        {
            XPCollection<CategorySub> _xpCategorySub = new XPCollection<CategorySub>(KetNoiCSDL.session);
            _xpCategorySub.Filter = FilterAll(admin);
            return _xpCategorySub;
        }
        /// <summary>
        /// public static CategorySub GetObjectByid(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static CategorySub GetObjectByid(int id,bool admin)
        {
            XPCollection<CategorySub> _xpCategorySub = new XPCollection<CategorySub>(KetNoiCSDL.session);
            _xpCategorySub.Filter = FilterById(id, admin);
            if (_xpCategorySub.Count > 0)
            {
                return _xpCategorySub[0];
            }
            return null;
        }
        public static XPCollection<CategorySub> GetCategorySubByIdCat(int idCat,bool admin)
        {
            XPCollection<CategorySub> _xpCategorySub = new XPCollection<CategorySub>(KetNoiCSDL.session);
            _xpCategorySub.Filter = FilterParamTypeInt(ColumCategorySub._Category,idCat, true);
            _xpCategorySub.Sorting = SetupSort(ColumCategorySub._Priority, true);
            return _xpCategorySub;
        }
        public static bool TestExistName(string columCatName, int vauleCat,string nameColum, string name, bool admin)
        {
            XPCollection<CategorySub> _xpCategorySub = new XPCollection<CategorySub>(KetNoiCSDL.session);
            if (admin == false)
                _xpCategorySub.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(columCatName + ".Id=" + vauleCat + " and Active=true and " + nameColum + "='" + name+"'");
            else
                _xpCategorySub.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(columCatName + ".Id=" + vauleCat + " and " + nameColum + "='" + name + "'");
           
            if (_xpCategorySub.Count > 0)
            {
                return true;
            }
            return false;

        }
        public static string GetContentByLanguage(string columName, CategorySub catsub)
        {
            switch (columName)
            {
                case ColumCategorySub._Name:
                    {
                        if (LanguageVN == true)
                            return catsub.Name;
                        return catsub.NameEL;
                    }
            }
            return "";
        }
        /// <summary>
        /// 26-06
        /// Xóa  hết các product của sub category
        /// </summary>
        /// <param name="idSubCat"></param>
        public static void DeleteAllCatSubOfCat(int idCat)
        {
            XPCollection<CategorySub> xpCatSub = new XPCollection<CategorySub>(KetNoiCSDL.session);
            xpCatSub = CategorySubDB.GetCategorySubByIdCat(idCat, true);
            while (xpCatSub.Count > 0)
            {
                CategorySubDB.DeleteCatSub(xpCatSub[0]);
            }
        }
   
       
        public static int DeleteCatSub(CategorySub obj)
        {
            if (obj != null)
            {
               ProductDB.DeleteAllProductOfSubCat(obj.Id);
                KetNoiCSDL.session.Delete(obj);
                return 1;
            }
            return 0;
        }
    }
}
